package com.lt.hello;

import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author lucine
 * @version 1.0 2021/6/20 14:20
 */
@Component
public class HelloCustomer {

    public static final Logger logger = LoggerFactory.getLogger(HelloCustomer.class);

    int i = 0;
    @RabbitHandler
    @RabbitListener(
            queuesToDeclare = @Queue(value = HelloWorldConfig.HELLO_WORLD_QUEUE_NAME
                            ,declare = "true",autoDelete = "false")
            )
    public void receive(Message message, Channel channel) throws Exception {
        System.out.println("HelloWorldConfig.HELLO_WORLD_QUEUE_NAME:" + message.getMessageProperties().getDeliveryTag() + new String(message.getBody()));
        try {
            if(new String(message.getBody()).equals("1")){
                int i = 1/0;
            }
            logger.info(HelloWorldConfig.HELLO_WORLD_QUEUE_NAME + "receive" +
                    message.getMessageProperties().getDeliveryTag() + ":接收成功");
            //消息处理成功回调
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e) {
            //配置了重试，但是重试3次后怎么处理对应的消息呢？？
            //消息处理失败回调
//            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            //todo 入库人工接入处理
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            logger.info(HelloWorldConfig.HELLO_WORLD_QUEUE_NAME + "receive" +
                    message.getMessageProperties().getDeliveryTag() + ":接收失败，并拒绝");
        }
    }
}
